<?php

/**
 * @method getParam
 * @method getParams
 * @method setParam
 * @method setParams
 * @method addParams
 * @method hasParam
 * @method getCmd
 * @method setCmd
 * @method getIdentifier
 * @method getHttpHeaders
 * @method getHttpHeader
 * @method isMultipart
 * @method isMethod
 * @method isPost
 * @method getFiles
 * @method setFiles
 * @method getLang
 */
class My_Request extends Core_Singleton {
    const OS_ANDROID = 'a';
    const OS_IPHONE = 'i';
    const OS_IPAD = 'p';
    const OS_ANDROID_TV = 'm';
    const OS_WEB = 'w';

    //v
    public static $clientVersionFull = '';
    public static $clientVersion = '';
    //vc
    public static $clientCustomId = '';
    public static $clientOs = '';
    //vd
    public static $UDID = '';
    public static $isTao = false;
    public static $isWeb = false;

    /* @var Core_Request */
    protected $request;

    /**
     *
     * @return My_Request
     */
    public static function getInstance() {
        return parent::getInstance();
    }

    protected function __construct() {
        parent::__construct();
        $this->request = Core_Request::getInstance();
        $v = $this->getParam('v', $this->getParam('ver'));
        if ($v) {
            self::$clientVersionFull = $v;
            //去掉debug标识等
            self::$clientVersion = preg_replace('~[^0-9\.]~', '', $v);
            self::$isTao = (substr($v, 0, 1) == 't');
            self::$clientOs = self::$isTao ? substr($v, 1, 1) : substr($v, 0, 1);
        }
        self::$clientCustomId = $this->getParam('vc', '');
        self::$UDID = $this->getParam('vd', '');
    }

    public function __call($method, $parameters) {
        return call_user_func_array([$this->request, $method], $parameters);
    }


    /**
     * 注意是gt 不是gte 相等时返回false
     *
     * @param string $ver valid ver like 4.1.5|4.0.0
     * @return bool
     */
    public static function isVersionGt($ver) {
        $ver = explode('.', $ver);
        $curr_ver = explode('.', self::$clientVersion);
        if (count($ver) != 3 || count($curr_ver) != 3) {
            return false;
        }
        if ($curr_ver[0] != $ver[0]) {
            return $curr_ver[0] > $ver[0];
        }
        if ($curr_ver[1] != $ver[1]) {
            return $curr_ver[1] > $ver[1];
        }
        if ($curr_ver[2] != $ver[2]) {
            return $curr_ver[2] > $ver[2];
        }

        return false;
    }

    public static function isVersionLt($ver) {
        $ver = explode('.', $ver);
        $curr_ver = explode('.', self::$clientVersion);
        if (count($ver) != 3 || count($curr_ver) != 3) {
            return false;
        }
        if ($curr_ver[0] != $ver[0]) {
            return $curr_ver[0] < $ver[0];
        }
        if ($curr_ver[1] != $ver[1]) {
            return $curr_ver[1] < $ver[1];
        }
        if ($curr_ver[2] != $ver[2]) {
            return $curr_ver[2] < $ver[2];
        }

        return false;
    }

    public static function isAndroid() {
        return self::$clientOs == self::OS_ANDROID;
    }

    public static function isIPhone() {
        return self::$clientOs == self::OS_IPHONE;
    }

    public static function isIPhonePro() {
        return self::isIPhone() && (false !== strpos(self::$clientVersionFull, 'p'));
    }

    public static function isIPad() {
        return self::$clientOs == self::OS_IPAD;
    }

    public static function isClient() {
        return self::isAndroid() || self::isIPhone() || self::isIPad();
    }

    public static function isWeb() {
        return self::$isWeb || self::$clientOs == self::OS_WEB;
    }

    public static function isTao() {
        return self::$isTao;
    }

}
